<?php

namespace yiike\store\merchant\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * OrderStoreSearch represents the model behind the search form about `yiike\store\models\OrderStore`.
 */
class OrderStoreSearch extends OrderStore
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'order_id', 'store_id', 'uid', 'is_pay', 'pay_at', 'consumer_code', 'status', 'is_refund', 'finished_at'], 'integer'],
            [['trade_id', 'uniontid', 'express_name', 'express_code', 'express_no', 'express_at'], 'safe'],
            [['total', 'quan', 'commision', 'settlement', 'credit1', 'credit2', 'credit3', 'credit4', 'credit5', 'credit3_pay', 'real_pay', 'refund_amount'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
// bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = OrderStore::find();
        $query->where(['store_id' => Yii::$app->store->id]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'order_id' => $this->order_id,
            'store_id' => $this->store_id,
            'uid' => $this->uid,
            'total' => $this->total,
            'quan' => $this->quan,
            'commision' => $this->commision,
            'settlement' => $this->settlement,
            'credit1' => $this->credit1,
            'credit2' => $this->credit2,
            'credit3' => $this->credit3,
            'credit4' => $this->credit4,
            'credit5' => $this->credit5,
            'credit3_pay' => $this->credit3_pay,
            'real_pay' => $this->real_pay,
            'is_pay' => $this->is_pay,
            'pay_at' => $this->pay_at,
            'consumer_code' => $this->consumer_code,
            'status' => $this->status,
            'is_refund' => $this->is_refund,
            'refund_amount' => $this->refund_amount,
            'express_at' => $this->express_at,
            'finished_at' => $this->finished_at,
        ]);

        $query->andFilterWhere(['like', 'trade_id', $this->trade_id])
            ->andFilterWhere(['like', 'uniontid', $this->uniontid])
            ->andFilterWhere(['like', 'express_name', $this->express_name])
            ->andFilterWhere(['like', 'express_code', $this->express_code])
            ->andFilterWhere(['like', 'express_no', $this->express_no]);

        return $dataProvider;
    }
}